<?php
class RegisterController extends BaseController{
    private $args;
    private $str;
    private $login = false;
    protected $error;

    public function index(){
        $eh = ErrorHandler::getInstance();
        $this->error = $eh->setException('empty', __LINE__, __FILE__);
        if(!empty($_SESSION)){
            ErrorHandler::setException('MultiEnter', __LINE__, __FILE__);
            header('Location: /WadOS/index/index');
        } else {
            $this->valid();
        }
    }

    public function valid($user_id = null){
        $err = '';
        $username='';
        $password='';
        $users = new UserModel();
        $val = Validator::getInstance();
        $val->validatePOST();

        if(isset($_POST)){
            $eh = ErrorHandler::getInstance();
            if($_POST['username'] != null){
                $username = $_POST['username'];
            } else {
                $this->error = $eh->setException('failUsername', __LINE__, __FILE__);
            }

            if($_POST['password'] != null && $_POST['rpassword'] != null){
                if($_POST['password'] === $_POST['rpassword']){
                    $password = $_POST['password'];
                } else {
                $this->error = $eh->setException('failRepeatPassword', __LINE__, __FILE__);
                }
            } else {
                $this->error = $eh->setException('failPassword', __LINE__, __FILE__);
            }
            $r = rand(0, 7000);
            if($username != '' && $password != ''){
                $users  ->setUsername($username)
                        ->setSalt($r)
                        ->setRole('registered')
                        ->setActive('1')
                        
                        ->setPassword(md5($r . $password));
                $users->save();
            }
        }
    
        $temp = Template::getInstance();
        $temp->set('errors', $this->error, true);
        $temp->show('header');
        $temp->show('edituser');
        $temp->show('footer');
    }
}
?>
